package com.zwb.service.controller;

import com.zwb.service.common.CacheConstant;
import com.zwb.service.pojo.Position;
import com.zwb.service.service.IPositionService;
import com.zwb.service.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 头发又黑又长
 * @Date 2022/9/1 20:23
 * @email zwb15083976291@163.com
 */
@Api(tags = "职位管理")
@RestController
@CacheConfig(cacheNames = CacheConstant.CACHEABLE_VALUE_3_DAT)
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    private IPositionService positionService;


    @ApiOperation("获取所有职位信息")

    @GetMapping("/")
    @Cacheable(key = "'position'")
    public R getAllPosition() {
        List<Position> list = positionService.list();
        return R.success(list);
    }


    @ApiOperation("添加职位信息")
    @CacheEvict(key = "'position'")
    @PostMapping("/")
    public R addPosition(@RequestBody Position position) {
        position.setCreateDate(new Date());
        if (positionService.save(position)) {
            return R.success("添加职位成功");
        } else {
            return R.error("添加失败");
        }
    }

    @ApiOperation("修改职位信息")
    @CacheEvict(key = "'position'")
    @PutMapping("/")
    public R updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return R.success("修改职位信息成功");
        } else {
            return R.error("修改职位信息失败");
        }
    }

    @ApiOperation("删除职位信息")
    @CacheEvict(key = "'position'")
    @DeleteMapping("/{id}")
    public R deletePosition(@PathVariable("id") long id) {
        if (positionService.removeById(id)) {
            return R.success("删除职位信息成功");
        } else {
            return R.error("删除职位信息失败");
        }
    }

    @ApiOperation("批量删除职位信息")
    @CacheEvict(key = "'position'")
    @DeleteMapping("/")
    public R deletePositionByIds(@RequestBody ArrayList<Long> ids) {
        if (positionService.removeBatchByIds(ids)) {
            return R.success("批量删除职位信息成功");
        } else {
            return R.error("批量删除职位信息失败");
        }
    }
}
